//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package net.mingsoft.basic.action;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import io.swagger.annotations.Api;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.MissingResourceException;
import javax.servlet.http.HttpServletRequest;
import net.mingsoft.base.util.SqlInjectionUtil;
import net.mingsoft.basic.biz.IAppBiz;
import net.mingsoft.basic.constant.e.CookieConstEnum;
import net.mingsoft.basic.constant.e.SessionConstEnum;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.config.MSProperties;
import org.apache.commons.collections.map.CaseInsensitiveMap;
import org.springframework.beans.factory.annotation.Autowired;

@Api("基础应用层的父类base")
public abstract class BaseAction extends net.mingsoft.base.action.BaseAction {
    @Autowired
    private IAppBiz appBiz;

    public BaseAction() {
    }

    protected String getResString(String key) {
        String str = "";

        try {
            str = super.getResString(key);
        } catch (MissingResourceException var4) {
            str = this.getLocaleString(key, "net.mingsoft.basic.resources.resources");
        }

        return str;
    }

    protected boolean checkRandCode() {
        return this.checkRandCode(SessionConstEnum.CODE_SESSION.toString());
    }

    protected String decryptByAES(HttpServletRequest request, String str) {
        String _str = SecureUtil.aes(SecureUtil.md5(BasicUtil.getApp().getAppId() + "").substring(16).getBytes()).decryptStr(str);
        return _str;
    }

    protected String encryptByAES(HttpServletRequest request, String str) {
        String _str = SecureUtil.aes(SecureUtil.md5(BasicUtil.getApp().getAppId() + "").substring(16).getBytes()).encryptHex(str);
        return _str;
    }

    protected String getRandCode() {
        return BasicUtil.getSession(SessionConstEnum.CODE_SESSION) + "";
    }

    protected String redirectBack(boolean flag) {
        return flag ? "redirect:" + BasicUtil.getCookie(CookieConstEnum.BACK_COOKIE) : BasicUtil.getCookie(CookieConstEnum.BACK_COOKIE);
    }

    protected boolean checkRandCode(String param) {
        boolean checkCode = MSProperties.manager.checkCode;
        if (!checkCode) {
            return true;
        } else {
            String sessionCode = this.getRandCode();
            String requestCode = BasicUtil.getString(param);
            this.LOG.debug("session_code:" + sessionCode + " requestCode:" + requestCode);
            if ("null".equals(sessionCode)) {
                return false;
            } else if (sessionCode.equalsIgnoreCase(requestCode)) {
                BasicUtil.removeSession(SessionConstEnum.CODE_SESSION);
                return true;
            } else {
                return false;
            }
        }
    }

    /** @deprecated */
    @Deprecated
    protected void removeUrlParams(HttpServletRequest request, String[] fitlers) {
        request.setAttribute("params", BasicUtil.assemblyRequestUrlParams(fitlers));
    }

    protected boolean validated(String tableName, String fieldName, String fieldValue) {
        SqlInjectionUtil.filterContent(new String[]{tableName, fieldName, fieldValue});
        Map where = new HashMap(1);
        where.put(fieldName, fieldValue);
        List list = this.appBiz.queryBySQL(tableName, (List)null, where);
        return ObjectUtil.isNotNull(list) && !list.isEmpty();
    }

    protected boolean validated(String tableName, String fieldName, String fieldValue, String id, String idName) {
        SqlInjectionUtil.filterContent(new String[]{tableName, fieldName, fieldValue});
        Map where = new HashMap(1);
        where.put(fieldName, fieldValue);
        List<CaseInsensitiveMap> list = this.appBiz.queryBySQL(tableName, (List)null, where);
        if (ObjectUtil.isNotNull(list) && !list.isEmpty()) {
            if (list.size() == 1) {
                CaseInsensitiveMap _map = new CaseInsensitiveMap((Map)list.get(0));
                if (id.equals(_map.get(idName).toString())) {
                    return false;
                }
            }

            return true;
        } else {
            return false;
        }
    }
}
